package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Orders;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.OrdersMapper;
import cn.marinda.resturants.service.OrderService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/** 订单实现类
 *  订单实现类
 * @date 2021/12/2
 * @version 1.0
 * @author Marinda
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper,Orders> implements OrderService {


    /**
     * 获取所有订单项
     */
    @Override
    public List<Orders> queryList() {
        return baseMapper.selectList(null);
    }

    /**
     * 选择uid
     *
     * @param uid uid
     * @return {@link Orders}
     */
    @Override
    public Orders selectUid(String uid) {
        return baseMapper.selectById(uid);
    }

    /**
     * 添加订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public boolean addOrder(Orders orders) {
        orders.setUid(UUID.randomUUID().toString());
        return baseMapper.insert(orders)>0;
    }

    /**
     * 删除订单
     *
     * @param uid 订单
     * @return 结果
     */
    @Override
    public boolean deleteOrder(String uid) {
        return  baseMapper.deleteById(uid)>0;
    }

    /**
     * 修改订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public boolean updateOrder(Orders orders) {
        Orders sqlOrders = baseMapper.selectById(orders.getUid());
        System.out.println("前：" + orders);
        if(sqlOrders == null){
            return false;
        }
        orders.setVersion(sqlOrders.getVersion());
        System.out.println("更改Version： "+ orders.getVersion());
        return lambdaUpdate().eq(Orders::getUid,orders.getUid()).eq(Orders::getVersion,orders.getVersion()).set(Orders::getVersion,orders.getVersion()+1).update(orders);
    }

    /**
     * 搜索指定台号的订单信息
     *
     * @param id 台号id
     * @return 订单信息
     */
    @Override
    public Orders selectId(String id) {
        return baseMapper.selectId(id);
    }

    /**
     * 通过订单uid找到用户id
     *
     * @param uid 用户uid
     * @return 订单信息
     */
    @Override
    public String selectOrderUid(String uid) {
        return baseMapper.selectOrderUid(uid);
    }

    /**
     * 查询订单开始时间和订单结束时间的区间订单列
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 列表订单
     */
    @Override
    public List<Orders> selectStartDateAndEndDateInterval(String startDate, String endDate) {
        return baseMapper.selectStartDateAndEndDateInterval(startDate,endDate);
    }

    /**
     * 查询这个用户的所有订单项
     *
     * @param user_uid userUid
     * @return 订单列
     */
    @Override
    public List<Orders> selectUserOrderList(String user_uid) {
        return baseMapper.selectUserOrderList(user_uid);
    }

    /**
     * 查询指定uuid的订单的总消费金额
     *
     * @param uid uid
     * @return 总消费金额
     */
    @Override
    public BigDecimal selectOrderNumber(String uid) {
        return baseMapper.selectOrderNumber(uid);
    }

    /**
     * 订单分页
     *
     * @param pages 分页
     * @return 订单分页结果
     */
    @Override
    public IPage<Orders> selectOrderPage(Pages<?> pages) {
        return baseMapper.selectOrderPage(pages);
    }


}
